package response

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

type Response struct {
	Code int         `json:"code"`
	Data interface{} `json:"object"`
	Msg  string      `json:"message"`
}

const (
	ERROR               = 7   //通用错误
	ERROR_NO_USER       = 10  //用户未登录
	ERROR_TOKEN_EXPIRED = 11  // token过期
	ERROR_TOKEN_REFRESH = 12  // token刷新失败
	USER_ROLES_ERR      = 13  // 角色权限不足
	SUCCESS             = 200 //成功
)

func Result(code int, data interface{}, msg string, c *gin.Context) {
	// 开始时间
	c.JSON(http.StatusOK, Response{
		code,
		data,
		msg,
	})
}

func Ok(c *gin.Context) {
	Result(SUCCESS, map[string]interface{}{}, "操作成功", c)
}

func OkWithMessage(message string, c *gin.Context) {
	Result(SUCCESS, map[string]interface{}{}, message, c)
}

func OkWithData(data interface{}, c *gin.Context) {
	Result(SUCCESS, data, "查询成功", c)
}

func OkWithDetailed(data interface{}, message string, c *gin.Context) {
	Result(SUCCESS, data, message, c)
}

func Fail(c *gin.Context) {
	Result(ERROR, map[string]interface{}{}, "操作失败", c)
}

func FailWithMessage(message string, c *gin.Context) {
	Result(ERROR, map[string]interface{}{}, message, c)
}

func FailWithDetailed(data interface{}, message string, c *gin.Context) {
	Result(ERROR, data, message, c)
}
func FailWithDetailedWithErrorCode(code int, data interface{}, message string, c *gin.Context) {
	Result(code, data, message, c)
}
